package com.hy.hyaudio.opensles

import android.icu.text.SimpleDateFormat
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.hy.hyaudio.databinding.ActivityOpenslEsRecorderBinding
import com.hy.opensles.HYOpenSlRecorder
import java.io.File

/**
 * @Description:
 * @Author: wdk
 * @CreateTime: 2025/1/3 23:00
 */
class OpenSLESRecorderActivity : AppCompatActivity() {

    companion object {
        private const val TAG = "OpenSLESRecorder"
    }

    private lateinit var binding: ActivityOpenslEsRecorderBinding
    private val hyOpenSlRecorder by lazy { HYOpenSlRecorder() }
    private var handler: Long = 0L

    //录音中
    private var recording = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityOpenslEsRecorderBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initListener()
    }

    private fun initListener() {
        //初始化OpenSL ES资源
        binding.initRecordOpenSLES.setOnClickListener {
            if (handler != 0L) {
                Toast.makeText(this, "资源已经初始化了", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            handler = hyOpenSlRecorder.CreateInstance()
            Log.i(TAG, "CreateInstance: handler=$handler")
            if (handler != 0L) {
                hyOpenSlRecorder.Init(handler)
            }
        }

        //开始录音，
        binding.startRecordOpenSLES.setOnClickListener {
            if (handler == 0L) {
                Toast.makeText(this, "请先初始化资源", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            if (recording) {
                Toast.makeText(this, "录音中，请先结束录音", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            val simpleDateFormat = SimpleDateFormat("yyyyMMdd_HHmmss")
            val dateStr = simpleDateFormat.format(System.currentTimeMillis())
            val pcmDirPath = "${filesDir.absolutePath}${File.separator}recorder"
            val dir=File(pcmDirPath)
            if (!dir.exists()){
                dir.mkdirs()
            }
            val pcmPath = "${pcmDirPath}${File.separator}${dateStr}.pcm"
            val file = File(pcmPath)
            file.createNewFile()
            hyOpenSlRecorder.StartRecord(handler, pcmPath)
            recording = true
        }


        //结束录音
        binding.stopRecordOpenSLES.setOnClickListener {
            if (handler == 0L) {
                Toast.makeText(this, "请先初始化资源", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            if (!recording) {
                Toast.makeText(this, "您还未开始录音", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            hyOpenSlRecorder.StopRecord(handler)
            recording = false
        }


        //释放OpenSL ES资源
        binding.releaseRecordOpenSLES.setOnClickListener {
            if (handler == 0L) {
                Toast.makeText(this, "您还未初始化资源", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            if (recording) {
                Toast.makeText(this, "录音中，请先结束录音", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            hyOpenSlRecorder.Release(handler)
            handler = 0L
        }

    }


}